package com.gxg.common.redis.impl;

import com.gxg.common.redis.JedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

@Service
public class JedisClientSingle implements JedisClient {

    @Autowired
    private JedisPool jedisPool;

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.get(key);
        jedis.close();
        return string;
    }

    @Override
    public Collection<String> scan(String key) {
        TreeSet<String> keys = new TreeSet<>();
        Jedis jedis = jedisPool.getResource();
        String scanRet = "0";
        ScanParams scanParams = new ScanParams();
        // 设置每次scan个数
        scanParams.count(1000);
        try {
            do {
                keys.addAll(jedis.scan(scanRet, scanParams.match(key)).getResult());
            } while (!scanRet.equals("0"));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        return keys;
    }

    @Override
    public Collection<String> sbkeys(String key) {
        Jedis jedis = jedisPool.getResource();
        Collection<String> string = jedis.keys(key);
        jedis.close();
        return string;
    }

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.close();
        return string;
    }

    @Override
    public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }

    @Override
    public long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }

    @Override
    public long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public long expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }

    @Override
    public long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public long del(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedis.close();
        return result;
    }

    @Override
    public long hdel(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(hkey, key);
        jedis.close();
        return result;
    }

    @Override
    public Map<String, String> hgetAll(String hkey) {
        Jedis jedis = jedisPool.getResource();
        Map<String, String> map = jedis.hgetAll(hkey);
        jedis.close();
        return map;
    }

    @Override
    public List<String> hmget(String hkey, String... fields) {
        Jedis jedis = jedisPool.getResource();
        List<String> list = jedis.hmget(hkey, fields);
        jedis.close();
        return list;
    }

    @Override
    public long decr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.decr(key);
        jedis.close();
        return result;
    }
}